package org.atfeihu.springcloud.java8;

import org.atfeihu.springcloud.entity.MyTestEntity;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * consumer接口就是一个消费型的接口，通过传入参数，然后输出值，就是这么简单
 *
 * @author wangfeihu
 * @create 2021/9/5 2:44
 */
public class ConsumerDemo {

    @Test
    public void testConsumer() {
        // 使用consumer接口实现方法
        Consumer<String> consumer = new Consumer<String>() {

            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Stream<String> stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        stream.forEach(consumer);
        System.out.println("==================================");

        Consumer<String> consumer2 = System.out::println;
        Stream.of("111", "222", "333", "444", "555", "666").forEach(consumer2);
        System.out.println("==================================");

        Stream.of("qqq", "www", "eee", "rrr", "ttt", "yyy").forEach(System.out::println);
        System.out.println("==================================");

        ArrayList<MyTestEntity> list = new ArrayList<>();
        list.add(new MyTestEntity(1001L, "王小虎1"));
        list.add(new MyTestEntity(1002L, "王小虎2"));
        list.add(new MyTestEntity(1003L, "王小虎3"));
        list.add(new MyTestEntity(1004L, "王小虎4"));
        /*Consumer<MyTestEntity> consumer3 = c -> System.out.println(c.getId());
        list.stream().forEach(consumer3);*/
        list.stream().forEach(new Consumer<MyTestEntity>() {
            @Override
            public void accept(MyTestEntity myTestEntity) {
                System.out.println(myTestEntity.getId());
            }
        });
    }
}
